﻿using Microsoft.Cloud.API.AzurePack.Services;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace Microsoft.Cloud.API.AzurePack.Controllers
{
    public class SubscriptionsController : ApiController
    {
        /// <summary>
        /// Retrieves a collection of subscriptions
        /// </summary>
        /// <returns></returns>
        [Route("api/admin/Subscriptions")]
        public async Task<HttpResponseMessage> GetSubscriptions()
        {
            var token = AuthService.Instance.PerformADFSAuth();

            using (var adminService = new WAPAdminService(token))
            {
                var response = await adminService.GetAsync(RelativePaths.Subscriptions_List);

                return response;
            }
        }

        /// <summary>
        /// Retrieves a subscription
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <returns></returns>
        [Route("api/admin/Subscriptions/{subscriptionId}")]
        public async Task<HttpResponseMessage> GetSubscription(string subscriptionId)
        {
            var token = AuthService.Instance.PerformADFSAuth();

            using (var adminService = new WAPAdminService(token))
            {
                var response = await adminService.GetAsync(string.Format(RelativePaths.Subscriptions_Get, subscriptionId));

                return response;
            }
        }

        /// <summary>
        /// Retrieves a subscription
        /// </summary>
        /// <param name="subscriptionId"></param>
        /// <returns></returns>
        [Route("api/tenant/{principal}/Subscriptions/{subscriptionId}")]
        public async Task<HttpResponseMessage> GetSubscription(string principal, string subscriptionId)
        {
            var token = AuthService.Instance.PerformASPAuth();

            using (var tenantService = new WAPTenantService(principal, token))
            {
                var response = await tenantService.GetAsync(string.Format(RelativePaths.Subscriptions_Get, subscriptionId));

                return response;
            }
        }
    }
}
